package com.thinkgem.jeesite.modules.bu.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.bu.entity.BuCustomer;
import com.thinkgem.jeesite.modules.bu.service.BuCustomerService;

/**
 * @author 
 * @version 2018-08-26
 */
@Controller
@RequestMapping(value = "${adminPath}/bu/buCustomer")
public class BuCustomerController extends BaseController {

	@Autowired
	private BuCustomerService buCustomerService;
	
	@ModelAttribute
	public BuCustomer get(@RequestParam(required=false) String id) {
		BuCustomer entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = buCustomerService.get(id);
		}
		if (entity == null){
			entity = new BuCustomer();
		}
		return entity;
	}
	
	@RequiresPermissions("bu:buCustomer:view")
	@RequestMapping(value = {"list", ""})
	public String list(BuCustomer buCustomer, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<BuCustomer> page = buCustomerService.findPage(new Page<BuCustomer>(request, response), buCustomer); 
		model.addAttribute("page", page);
		return "modules/bu/buCustomerList";
	}

	@RequiresPermissions("bu:buCustomer:view")
	@RequestMapping(value = "form")
	public String form(BuCustomer buCustomer, Model model) {
		model.addAttribute("buCustomer", buCustomer);
		return "modules/bu/buCustomerForm";
	}

	@RequiresPermissions("bu:buCustomer:edit")
	@RequestMapping(value = "save")
	public String save(BuCustomer buCustomer, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, buCustomer)){
			return form(buCustomer, model);
		}
		buCustomerService.save(buCustomer);
		addMessage(redirectAttributes, "保存单表成功");
		return "redirect:"+Global.getAdminPath()+"/bu/buCustomer/?repage";
	}
	
	@RequiresPermissions("bu:buCustomer:edit")
	@RequestMapping(value = "delete")
	public String delete(BuCustomer buCustomer, RedirectAttributes redirectAttributes) {
		buCustomerService.delete(buCustomer);
		addMessage(redirectAttributes, "删除单表成功");
		return "redirect:"+Global.getAdminPath()+"/bu/buCustomer/?repage";
	}

}